/*
 * @Author: wz
 * @Date: 2020-11-25 23:17:39
 * @LastEditors: wz
 * @LastEditTime: 2021-01-29 15:24:42
 * @Description: vue 配置信息
 */

const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin')
const resolve = dir => path.join(__dirname, dir)
const isDevelopment = process.env.NODE_ENV === 'development'
const mockServer = () => {
    if (isDevelopment) return require('./mock')
    else return ''
}

module.exports = {
    publicPath: './', // 公共路径
    outputDir: 'dist', // 生产环境构建文件的目录
    lintOnSave: false, // 关闭eslint
    assetsDir: '',
    productionSourceMap: false,
    configureWebpack: config => {
        config.resolve.alias = {
            '@': resolve('src')
        }
        if (!isDevelopment) {
            // 关闭console
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
            // 打包gzip
            const compression = new CompressionPlugin({
                filename: '[path].gz[query]',
                algorithm: 'gzip',
                test: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i,
                threshold: 10240,
                minRatio: 0.8
            })
            config.plugins = [...config.plugins, compression]
        }
    },
    chainWebpack: config => {
        config.plugins.delete('prefetch')
        config.plugins.delete('preload')
    },
    css: {
        // loaderOptions: {
        //     less: {
        //         // less 不用导入，可直接使用
        //         additionalData: `
        //             @import '@/src/css/mixin.less';
        //             `
        //     }
        // },
        // 是否使用css分离插件
        extract: true
    },
    devServer: {
        // 配置服务器
        port: 5000, // 端口
        open: true, // 在服务器启动后打开浏览器，将其设置true为打开默认浏览器
        https: false, // 是否用https请求
        overlay: {
            // 在编译器错误或警告时，浏览器中显示全屏覆盖
            warnings: true,
            errors: true
        },
        before: mockServer()
        /*  proxy: {
            '/api': {
                target: 'http://47.100.186.132/your-path/api',
                changeOrigin: true,
                pathRewrite: {
                    '^/api': '',
                },
            },
        }, */
    },
    pluginOptions: {
        // 第三方插件配置
        'style-resources-loader': {
            preProcessor: 'less',
            patterns: [path.resolve(__dirname, './src/assets/css/mixin.less')]
        }
    }
}
